#include "global.h"
#include <cassert>

#include "Sprite.h"
#include "RageTextureManager.h"
#include "XmlFile.h"
#include "RageLog.h"
#include "RageDisplay.h"
#include "RageTexture.h"
#include "RageUtil.h"
#include "ActorUtil.h"
#include "arch/Dialog/Dialog.h"
#include "Foreach.h"
#include "LuaBinding.h"
#include "LuaManager.h"

REGISTER_ACTOR_CLASS( Sprite )


Sprite::Sprite()
{
	m_pTexture = NULL;
	m_iCurState = 0;
	m_fSecsIntoState = 0.0f;
	m_bUsingCustomTexCoords = false;
	m_bSkipNextUpdate = true;
	m_EffectMode = EffectMode_Normal;
	
	m_fRememberedClipWidth = -1;
	m_fRememberedClipHeight = -1;

	m_fTexCoordVelocityX = 0;
	m_fTexCoordVelocityY = 0;
}

Sprite::~Sprite()
{
	UnloadTexture();
}

Sprite::Sprite( const Sprite &cpy ):
	Actor( cpy )
{
#define CPY(a) a = cpy.a
	CPY( m_States );
	CPY( m_iCurState );
	CPY( m_fSecsIntoState );
	CPY( m_bUsingCustomTexCoords );
	CPY( m_bSkipNextUpdate );
	CPY( m_EffectMode );
	memcpy( m_CustomTexCoords, cpy.m_CustomTexCoords, sizeof(m_CustomTexCoords) );
	CPY( m_fRememberedClipWidth );
	CPY( m_fRememberedClipHeight );
	CPY( m_fTexCoordVelocityX );
	CPY( m_fTexCoordVelocityY );
#undef CPY

	if( cpy.m_pTexture != NULL )
		m_pTexture = TEXTUREMAN->CopyTexture( cpy.m_pTexture );
	else
		m_pTexture = NULL;
}

void Sprite::InitState()
{
	Actor::InitState();
	m_iCurState = 0;
	m_fSecsIntoState = 0.0f;
	m_bSkipNextUpdate = true;
}


RageTextureID Sprite::SongBGTexture( RageTextureID ID )
{
	ID.bMipMaps = true;

	ID.iAlphaBits = 0;

	ID.Policy = RageTextureID::TEX_VOLATILE;

	ID.bDither = true;

	return ID;
}

RageTextureID Sprite::SongBannerTexture( RageTextureID ID )
{
	ID.bHotPinkColorKey = true;
	ID.iColorDepth = 32;
	ID.Policy = RageTextureID::TEX_VOLATILE;

	return ID;
}

void Sprite::Load( RageTextureID ID )
{
	if( !ID.filename.empty() ) 
		LoadFromTexture( ID );

	LoadStatesFromTexture();
};

void Sprite::LoadFromNode( const XNode* pNode )
{
	RString sPath;
	pNode->GetAttrValue( "Texture", sPath );
	if( !sPath.empty() && !TEXTUREMAN->IsTextureRegistered( RageTextureID(sPath) ) )
		ActorUtil::GetAttrPath( pNode, "Texture", sPath );

	if( !sPath.empty() )
	{
		LoadFromTexture( sPath );
		
		LoadStatesFromTexture();

		vector<State> aStates;

		const XNode *pFrames = pNode->GetChild( "Frames" );
		if( pFrames != NULL )
		{
			int iFrameIndex = 0;
			for( int i=0; true; i++ )
			{
				const XNode *pFrame = pFrames->GetChild( ssprintf("%i", i+1) ); // +1 for Lua's arrays
				if( pFrame == NULL )
					break;

				State newState;
				if( !pFrame->GetAttrValue("Delay", newState.fDelay) )
					newState.fDelay = 0.1f;

				pFrame->GetAttrValue( "Frame", iFrameIndex );
				if( iFrameIndex >= m_pTexture->GetNumFrames() )
					RageException::Throw( "%s: State #%i is frame %d, but the texture \"%s\" only has %d frames",
						ActorUtil::GetWhere(pNode).c_str(), i, iFrameIndex, sPath.c_str(), m_pTexture->GetNumFrames() );
				newState.rect = *m_pTexture->GetTextureCoordRect( iFrameIndex );

				const XNode *pPoints[2] = { pFrame->GetChild( "1" ), pFrame->GetChild( "2" ) };
				if( pPoints[0] != NULL && pPoints[1] != NULL )
				{
					RectF r = newState.rect;

					float fX = 1.0f, fY = 1.0f;
					pPoints[0]->GetAttrValue( "x", fX );
					pPoints[0]->GetAttrValue( "y", fY );
					newState.rect.left = SCALE( fX, 0.0f, 1.0f, r.left, r.right );
					newState.rect.top = SCALE( fY, 0.0f, 1.0f, r.top, r.bottom );

					pPoints[1]->GetAttrValue( "x", fX );
					pPoints[1]->GetAttrValue( "y", fY );
					newState.rect.right = SCALE( fX, 0.0f, 1.0f, r.left, r.right );
					newState.rect.bottom = SCALE( fY, 0.0f, 1.0f, r.top, r.bottom );
				}

				aStates.push_back( newState );
			}
		}
		else for( int i=0; true; i++ )
		{
			RString sFrameKey = ssprintf( "Frame%04d", i );
			RString sDelayKey = ssprintf( "Delay%04d", i );
			State newState;

			int iFrameIndex;
			if( !pNode->GetAttrValue(sFrameKey, iFrameIndex) )
				break;
			if( iFrameIndex >= m_pTexture->GetNumFrames() )
				RageException::Throw( "%s: %s is %d, but the texture \"%s\" only has %d frames",
					ActorUtil::GetWhere(pNode).c_str(), sFrameKey.c_str(), iFrameIndex, sPath.c_str(), m_pTexture->GetNumFrames() );

			newState.rect = *m_pTexture->GetTextureCoordRect(iFrameIndex);
			if (!pNode->GetAttrValue(sDelayKey, newState.fDelay))
				break;
			aStates.push_back(newState);
		}

		if (!aStates.empty())
		{
			m_States = aStates;
			Sprite::m_size.x = aStates[0].rect.GetWidth() / m_pTexture->GetSourceToTexCoordsRatioX();
			Sprite::m_size.y = aStates[0].rect.GetHeight() / m_pTexture->GetSourceToTexCoordsRatioY();
		}
	}

	Actor::LoadFromNode(pNode);
}

void Sprite::UnloadTexture()
{
	if (m_pTexture != NULL)
	{
		TEXTUREMAN->UnloadTexture(m_pTexture);
		m_pTexture = NULL;

		SetState(0);
	}
}

void Sprite::EnableAnimation(bool bEnable)
{
	bool bWasEnabled = m_bIsAnimating;
	Actor::EnableAnimation(bEnable);

	if (bEnable && !bWasEnabled)
	{
		m_bSkipNextUpdate = true;
	}
}

void Sprite::SetTexture(RageTexture* pTexture)
{
	ASSERT(pTexture != NULL);

	if (m_pTexture != pTexture)
	{
		UnloadTexture();
		m_pTexture = pTexture;
	}

	ASSERT(m_pTexture->GetTextureWidth() >= 0);
	ASSERT(m_pTexture->GetTextureHeight() >= 0);

	if( m_fRememberedClipWidth != -1 && m_fRememberedClipHeight != -1 )
		ScaleToClipped( m_fRememberedClipWidth, m_fRememberedClipHeight );

	if( m_States.empty() )
		LoadStatesFromTexture();
}

void Sprite::LoadFromTexture( RageTextureID ID )
{
	RageTexture *pTexture = NULL;
	if( m_pTexture && m_pTexture->GetID() == ID )
		pTexture = m_pTexture;
	else
		pTexture = TEXTUREMAN->LoadTexture( ID );

	SetTexture( pTexture );
}

void Sprite::LoadStatesFromTexture()
{
	m_States.clear();

	if( m_pTexture == NULL )
	{
		State newState;
		newState.fDelay = 0.1f;
		newState.rect = RectF( 0, 0, 1, 1 );
		m_States.push_back( newState );
		return;		
	}
	
	for( int i=0; i<m_pTexture->GetNumFrames(); ++i )
	{
		State newState;
		newState.fDelay = 0.1f;
		newState.rect = *m_pTexture->GetTextureCoordRect( i );
		m_States.push_back( newState );
	}
}

void Sprite::UpdateAnimationState()
{

	if( m_States.size() > 1 )
	{
		while( m_fSecsIntoState+0.0001f > m_States[m_iCurState].fDelay )	
		{
			m_fSecsIntoState -= m_States[m_iCurState].fDelay;
			m_iCurState = (m_iCurState+1) % m_States.size();
		}
	}
}

void Sprite::Update( float fDelta )
{
	Actor::Update( fDelta );

	const bool bSkipThisMovieUpdate = m_bSkipNextUpdate;
	m_bSkipNextUpdate = false;

	if( !m_bIsAnimating )
		return;

	if( !m_pTexture )	
	    return;

	float fTimePassed = GetEffectDeltaTime();
	m_fSecsIntoState += fTimePassed;

	if( m_fSecsIntoState < 0 )
		wrap( m_fSecsIntoState, GetAnimationLengthSeconds() );

	UpdateAnimationState();

	if( !bSkipThisMovieUpdate )
		m_pTexture->DecodeSeconds( max(0, fTimePassed) );

	if( m_fTexCoordVelocityX != 0 || m_fTexCoordVelocityY != 0 )
	{
		float fTexCoords[8];
		Sprite::GetActiveTextureCoords( fTexCoords );
 
		fTexCoords[0] += fDelta*m_fTexCoordVelocityX;
		fTexCoords[1] += fDelta*m_fTexCoordVelocityY; 
		fTexCoords[2] += fDelta*m_fTexCoordVelocityX;
		fTexCoords[3] += fDelta*m_fTexCoordVelocityY;
		fTexCoords[4] += fDelta*m_fTexCoordVelocityX;
		fTexCoords[5] += fDelta*m_fTexCoordVelocityY;
		fTexCoords[6] += fDelta*m_fTexCoordVelocityX;
		fTexCoords[7] += fDelta*m_fTexCoordVelocityY;

		if( m_bTextureWrapping )
		{
			const float fXAdjust = floorf( fTexCoords[0] );
			const float fYAdjust = floorf( fTexCoords[1] );
			fTexCoords[0] -= fXAdjust;
			fTexCoords[2] -= fXAdjust;
			fTexCoords[4] -= fXAdjust;
			fTexCoords[6] -= fXAdjust;
			fTexCoords[1] -= fYAdjust;
			fTexCoords[3] -= fYAdjust;
			fTexCoords[5] -= fYAdjust;
			fTexCoords[7] -= fYAdjust;
		}

		Sprite::SetCustomTextureCoords( fTexCoords );
	}
}

void TexCoordArrayFromRect( float fImageCoords[8], const RectF &rect )
{
	fImageCoords[0] = rect.left;	fImageCoords[1] = rect.top;	
	fImageCoords[2] = rect.left;	fImageCoords[3] = rect.bottom;
	fImageCoords[4] = rect.right;	fImageCoords[5] = rect.bottom;
	fImageCoords[6] = rect.right;	fImageCoords[7] = rect.top;	
}

void Sprite::DrawTexture( const TweenState *state )
{
	Actor::SetGlobalRenderStates();

	if( state->crop.left + state->crop.right >= 1  || 
		state->crop.top + state->crop.bottom >= 1 ) 
		return; 

	RectF quadVerticies;
	quadVerticies.left   = -m_size.x/2.0f;
	quadVerticies.right  = +m_size.x/2.0f;
	quadVerticies.top    = -m_size.y/2.0f;
	quadVerticies.bottom = +m_size.y/2.0f;

	RectF crop = state->crop;
	CLAMP( crop.left, 0, 1 );
	CLAMP( crop.right, 0, 1 );
	CLAMP( crop.top, 0, 1 );
	CLAMP( crop.bottom, 0, 1 );

	RectF croppedQuadVerticies = quadVerticies; 
#define IF_CROP_POS(side,opp_side) \
	if(state->crop.side!=0) \
		croppedQuadVerticies.side = \
			SCALE( crop.side, 0.f, 1.f, quadVerticies.side, quadVerticies.opp_side )
	IF_CROP_POS( left, right ); 
	IF_CROP_POS( top, bottom ); 
	IF_CROP_POS( right, left ); 
	IF_CROP_POS( bottom, top );

	static RageSpriteVertex v[4];
	v[0].p = RageVector3( croppedQuadVerticies.left,	croppedQuadVerticies.top,	0 );	
	v[1].p = RageVector3( croppedQuadVerticies.left,	croppedQuadVerticies.bottom,	0 );
	v[2].p = RageVector3( croppedQuadVerticies.right,	croppedQuadVerticies.bottom,	0 );
	v[3].p = RageVector3( croppedQuadVerticies.right,	croppedQuadVerticies.top,	0 );	

	DISPLAY->ClearAllTextures();
	DISPLAY->SetTexture( TextureUnit_1, m_pTexture? m_pTexture->GetTexHandle():0 );

	Actor::SetTextureRenderStates();	
	DISPLAY->SetEffectMode( m_EffectMode );

	if( m_pTexture )
	{
		float f[8];
		GetActiveTextureCoords( f );

		if( state->crop.left || state->crop.right || state->crop.top || state->crop.bottom )
		{
			RageVector2 texCoords[4] = {
				RageVector2( f[0], f[1] ),
				RageVector2( f[2], f[3] ),
				RageVector2( f[4], f[5] ),
				RageVector2( f[6], f[7] ) 
			};

			for( int i = 0; i < 4; ++i )
			{
				RageSpriteVertex *pVert = &v[i];

				float fTopX = SCALE( pVert->p.x, quadVerticies.left, quadVerticies.right, texCoords[0].x, texCoords[3].x );
				float fBottomX = SCALE( pVert->p.x, quadVerticies.left, quadVerticies.right, texCoords[1].x, texCoords[2].x );
				pVert->t.x = SCALE( pVert->p.y, quadVerticies.top, quadVerticies.bottom, fTopX, fBottomX );

				float fLeftY = SCALE( pVert->p.y, quadVerticies.top, quadVerticies.bottom, texCoords[0].y, texCoords[1].y );
				float fRightY = SCALE( pVert->p.y, quadVerticies.top, quadVerticies.bottom, texCoords[3].y, texCoords[2].y );
				pVert->t.y = SCALE( pVert->p.x, quadVerticies.left, quadVerticies.right, fLeftY, fRightY );
			}
		}
		else
		{
			v[0].t = RageVector2( f[0], f[1] );	
			v[1].t = RageVector2( f[2], f[3] );	
			v[2].t = RageVector2( f[4], f[5] );	
			v[3].t = RageVector2( f[6], f[7] );	
		}
	}
	else
	{
		for( unsigned i = 0; i < 4; ++i )
			v[i].t.x = v[i].t.y = 0;
	}

	if( state->diffuse[0].a > 0 || 
		state->diffuse[1].a > 0 ||
		state->diffuse[2].a > 0 ||
		state->diffuse[3].a > 0 )
	{
		DISPLAY->SetTextureMode( TextureUnit_1, TextureMode_Modulate );

		if( m_fShadowLengthX != 0  ||  m_fShadowLengthY != 0 )
		{
			DISPLAY->PushMatrix();
			DISPLAY->TranslateWorld( m_fShadowLengthX, m_fShadowLengthY, 0 );
			RageColor c = m_ShadowColor;
			c.a *= state->diffuse[0].a;
			v[0].c = v[1].c = v[2].c = v[3].c = c;
			DISPLAY->DrawQuad( v );
			DISPLAY->PopMatrix();
		}

		v[0].c = state->diffuse[0];
		v[1].c = state->diffuse[2];
		v[2].c = state->diffuse[3];
		v[3].c = state->diffuse[1];
		DISPLAY->DrawQuad( v );
	}

	if( state->glow.a > 0.0001f )
	{
		DISPLAY->SetTextureMode( TextureUnit_1, TextureMode_Glow );
		v[0].c = v[1].c = v[2].c = v[3].c = state->glow;
		DISPLAY->DrawQuad( v );
	}
	DISPLAY->SetEffectMode( EffectMode_Normal );
}

bool Sprite::EarlyAbortDraw() const
{
	return m_pTexture == NULL;
}

void Sprite::DrawPrimitives()
{
	if( m_pTempState->fade.top > 0 ||
		m_pTempState->fade.bottom > 0 ||
		m_pTempState->fade.left > 0 ||
		m_pTempState->fade.right > 0 )
	{
		const RectF &FadeDist = m_pTempState->fade;

		RectF FadeSize = FadeDist;

		const float HorizRemaining = 1.0f - (m_pTempState->crop.left + m_pTempState->crop.right);
		if( FadeDist.left+FadeDist.right > 0 &&
			HorizRemaining < FadeDist.left+FadeDist.right )
		{
			const float LeftPercent = FadeDist.left/(FadeDist.left+FadeDist.right);
			FadeSize.left = LeftPercent * HorizRemaining;
			FadeSize.right = (1.0f-LeftPercent) * HorizRemaining;
		}

		const float VertRemaining = 1.0f - (m_pTempState->crop.top + m_pTempState->crop.bottom);
		if( FadeDist.top+FadeDist.bottom > 0 &&
			VertRemaining < FadeDist.top+FadeDist.bottom )
		{
			const float TopPercent = FadeDist.top/(FadeDist.top+FadeDist.bottom);
			FadeSize.top = TopPercent * VertRemaining;
			FadeSize.bottom = (1.0f-TopPercent) * VertRemaining;
		}

		const float RightAlpha = SCALE(FadeSize.right, FadeDist.right, 0, 1, 0);
		const float LeftAlpha = SCALE(FadeSize.left, FadeDist.left, 0, 1, 0);
		const float TopAlpha = SCALE(FadeSize.top, FadeDist.top, 0, 1, 0);
		const float BottomAlpha = SCALE(FadeSize.bottom, FadeDist.bottom, 0, 1, 0);

		TweenState ts = *m_pTempState;
		ts.crop.left += FadeDist.left;
		ts.crop.right += FadeDist.right;
		ts.crop.top += FadeDist.top;
		ts.crop.bottom += FadeDist.bottom;
		DrawTexture(*ts);

		if (FadeSize.left > 0.001f)
		{
			ts.crop = m_pTempState->crop;
			ts.crop.right = 1 - (ts.crop.left + FadeSize.left);
			ts.crop.top += FadeDist.top;
			ts.crop.bottom += FadeDist.bottom;

			ts.diffuse[0] = m_pTempState->diffuse[0];
			ts.diffuse[2] = m_pTempState->diffuse[2];
			ts.diffuse[3] = m_pTempState->diffuse[2];
			ts.diffuse[1] = m_pTempState->diffuse[0];
			ts.diffuse[0].a = 0;
			ts.diffuse[2].a = 0;
			ts.diffuse[3].a *= LeftAlpha;
			ts.diffuse[1].a *= LeftAlpha;
			DrawTexture(*ts);
		}

		if (FadeRight.right > 0.001f)
		{
			ts.crop = m_pTempState->crop;
			ts.crop.left = 1 - (ts.crop.right + FadeSize.right);
			ts.crop.top += FadeDist.top;
			ts.crop.bottom += FadeDist.bottom;
			ts.diffuse[0] = m_pTempState->diffuse[1];
			ts.diffuse[2] = m_pTempState->diffuse[3];
			ts.diffuse[3] = m_pTempState->diffuse[3];
			ts.diffuse[1] = m_pTempState->diffuse[1];
			ts.diffuse[0].a *= RightAlpha;
			ts.diffuse[2].a *= RightAlpha;
			ts.diffuse[3].a = 0;
			ts.diffuse[1].a = 0;

			DrawTexture(*ts);
		}

		if( FadeSize.top > 0.001f )
		{
			ts.crop = m_pTempState->crop;
			ts.crop.bottom = 1 - (ts.crop.top + FadeSize.top);
			ts.crop.left += FadeDist.left;
			ts.crop.right += FadeDist.right;

			ts.diffuse[0] = m_pTempState->diffuse[0];
			ts.diffuse[2] = m_pTempState->diffuse[0];	
			ts.diffuse[3] = m_pTempState->diffuse[1];	
			ts.diffuse[1] = m_pTempState->diffuse[1];	
			ts.diffuse[0].a = 0;			
			ts.diffuse[2].a *= TopAlpha;			
			ts.diffuse[3].a *= TopAlpha;	
			ts.diffuse[1].a = 0;			

			DrawTexture( &ts );
		}

		if( FadeSize.bottom > 0.001f )
		{
			ts.crop = m_pTempState->crop;
			ts.crop.top = 1 - (ts.crop.bottom + FadeSize.bottom);
			ts.crop.left += FadeDist.left;
			ts.crop.right += FadeDist.right;

			ts.diffuse[0] = m_pTempState->diffuse[2];	
			ts.diffuse[2] = m_pTempState->diffuse[2];	
			ts.diffuse[3] = m_pTempState->diffuse[3];	
			ts.diffuse[1] = m_pTempState->diffuse[3];	
			ts.diffuse[0].a *= BottomAlpha;		
			ts.diffuse[2].a = 0;				
			ts.diffuse[3].a = 0;				
			ts.diffuse[1].a *= BottomAlpha;		
			DrawTexture( &ts );
		}
	}
	else
	{
		DrawTexture(m_pTempState);
	}
}

int Sprite::GetNumStates() const
{
	return m_States.size();
}

void Sprite::SetState(int iNewState)
{
	if (iNewState != 0 && (iNewState < 0 || iNewState >= (int)m_States.size()))
	{
		if (!m_pTexture || (m_pTexture->GetID().filename.find("_blank") == string::npos &&
			m_pTexture->GetID().filename.find("_missing") == string::npos))
		{
			RString sError;
			if (m_pTexture)
				sError = ssprintf("A Sprite '%s' (\"%s\") tried to set state index %d, but it has only %u states.",
					m_pTexture->GetID().filename.c_str(), this->m_sName.c_str(), iNewState, unsigned(m_States.size()));
			else
				sError = ssprintf("A Sprite (\"%s\") tried to set state index %d, but no texture is loaded.",
					this->m_sName.c_str(), iNewState);
			Dialog::OK(sError, "SPRITE_INVALID_FRAME");
		}
	}

	CLAMP(iNewState, 0, (int)m_States.size() - 1);
	m_iCurState = iNewState;
	m_fSecsInfoState = 0.0f;
}

foat Sprite::GetAnimationLengthSeconds() const
{
	float fTotal = 0;
	FOREACH_CONST(State, m_States, s)
		fTotal += s->fDelay;
	return fTotal;
}

void Sprite::SetSecondsIntoAnimation(float fSeconds)
{
	SetState(0);
	if( m_pTexture )
		m_pTexture->SetPosition( fSeconds );
	m_fSecsIntoState = fSeconds;
	UpdateAnimationState();
}

RString	Sprite::GetTexturePath() const
{
	if( m_pTexture==NULL )
		return RString();

	return m_pTexture->GetID().filename;
}

void Sprite::SetCustomTextureRect( const RectF &new_texcoord_frect ) 
{ 
	m_bUsingCustomTexCoords = true;
	m_bTextureWrapping = true;
	TexCoordArrayFromRect( m_CustomTexCoords, new_texcoord_frect );
}

void Sprite::SetCustomTextureCoords( float fTexCoords[8] )
{ 
	m_bUsingCustomTexCoords = true;
	m_bTextureWrapping = true;
	for( int i=0; i<8; i++ )
		m_CustomTexCoords[i] = fTexCoords[i]; 
}

void Sprite::SetCustomImageRect( RectF rectImageCoords )
{
	rectImageCoords.left	*= m_pTexture->GetImageWidth()	/ (float)m_pTexture->GetTextureWidth();
	rectImageCoords.right	*= m_pTexture->GetImageWidth()	/ (float)m_pTexture->GetTextureWidth();
	rectImageCoords.top	*= m_pTexture->GetImageHeight()	/ (float)m_pTexture->GetTextureHeight(); 
	rectImageCoords.bottom	*= m_pTexture->GetImageHeight()	/ (float)m_pTexture->GetTextureHeight(); 

	SetCustomTextureRect( rectImageCoords );
}

void Sprite::SetCustomImageCoords( float fImageCoords[8] )
{
	for( int i=0; i<8; i+=2 )
	{
		fImageCoords[i+0] *= m_pTexture->GetImageWidth()	/ (float)m_pTexture->GetTextureWidth(); 
		fImageCoords[i+1] *= m_pTexture->GetImageHeight()	/ (float)m_pTexture->GetTextureHeight(); 
	}

	SetCustomTextureCoords( fImageCoords );
}

const RectF *Sprite::GetCurrentTextureCoordRect() const
{
	return GetTextureCoordRectForState( m_iCurState );
}

const RectF *Sprite::GetTextureCoordRectForState( int iState ) const
{
	ASSERT_M( iState < (int) m_States.size(), ssprintf("%d, %d", int(iState), int(m_States.size())) );

	return &m_States[iState].rect;
}

void Sprite::GetActiveTextureCoords(float fTexCoordsOut[8]) const
{
	if (m_bUsingCustomTexCoords)
	{
		for (int i = 0; i < 8; i++)
			fTexCoordsOut[i] = m_CustomTexCoords[i];
	}
	else
	{
		const RectF* pTexCoordRect = GetCurrentTextureCoordRect();
		TexCoordArrayFromRect(fTexCoordsOut, *pTexCoordRect);
	}
}

void Sprite::StopUsingCustomCoords()
{
	m_bUsingCustomTexCoords = false;
}


